﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CursorColor
{
    public partial class CursorColor : Form
    {
        private static Label CursorPositionLab;
        private static Label CursorColorRGBLab;
        private static Label CursorColorHexLab;
        private Thread MainThread;
        private static volatile bool systemexit = false;
        private static volatile bool MouseDownFlag = false;
        private Point clickLocation; // 鼠标点击的位置
        public CursorColor()
        {
            InitializeComponent();
        }

        private void CursorColor_Load(object sender, EventArgs e)
        {
            this.Size = new Size(100, 62);
            this.FormBorderStyle = FormBorderStyle.None;
            this.AutoSize = true;
            this.BackColor = Color.FromArgb(46, 42, 65);
            
            Font MainFont = new Font("微软雅黑", 9, FontStyle.Bold);
            this.Font = MainFont;
            //string logopath = @".\Logo.ico";
            ////MessageBox.Show("程序的目录路径: " + logopath);
            //if (System.IO.File.Exists(logopath)) // 检查文件是否存在
            //{
            //    this.Icon = new Icon(logopath); // 从文件路径加载图标
            //}
            //this.ShowIcon = true;
            //this.IsMdiContainer = true;

            MainPanel.Size = this.Size;
            MainPanel.AutoSize = true;
            MainPanel.BorderStyle = BorderStyle.FixedSingle;
            MainPanel.Dock = DockStyle.Fill;
            // 位置
            CursorPositionLab = new Label
            {
                Dock = DockStyle.Fill,
                TextAlign = ContentAlignment.BottomLeft,
                ForeColor = Color.BurlyWood,
                BorderStyle = BorderStyle.FixedSingle,
                AutoSize = false
            };
            CursorPositionLab.MouseDown += new MouseEventHandler(MouseDownEventHandler);
            CursorPositionLab.MouseUp += new MouseEventHandler(MouseUpEventHandler);
            CursorPositionLab.MouseMove += new MouseEventHandler(MouseMoveEventHandler);
            CursorPositionLab.DoubleClick += new EventHandler(ExitHandler);
            this.MainPanel.Controls.Add(CursorPositionLab);
            // 颜色RGB
            CursorColorRGBLab = new Label
            {
                Dock = DockStyle.Top,
                TextAlign = ContentAlignment.BottomLeft,
                ForeColor = Color.BurlyWood,
                BorderStyle = BorderStyle.FixedSingle,
                AutoSize = false,
            };
            CursorColorRGBLab.MouseDown += new MouseEventHandler(MouseDownEventHandler);
            CursorColorRGBLab.MouseUp += new MouseEventHandler(MouseUpEventHandler);
            CursorColorRGBLab.MouseMove += new MouseEventHandler(MouseMoveEventHandler);
            CursorColorRGBLab.DoubleClick += new EventHandler(ExitHandler);
            this.MainPanel.Controls.Add(CursorColorRGBLab);
            // 颜色Hex
            CursorColorHexLab = new Label
            {
                Dock = DockStyle.Top,
                TextAlign = ContentAlignment.BottomLeft,
                ForeColor = Color.BurlyWood,
                BorderStyle = BorderStyle.FixedSingle,
                AutoSize = false,
            };
            CursorColorHexLab.MouseDown += new MouseEventHandler(MouseDownEventHandler);
            CursorColorHexLab.MouseUp += new MouseEventHandler(MouseUpEventHandler);
            CursorColorHexLab.MouseMove += new MouseEventHandler(MouseMoveEventHandler);
            CursorColorHexLab.DoubleClick += new EventHandler(ExitHandler);
            this.MainPanel.Controls.Add(CursorColorHexLab);
            // 标题
            Label Titel = new Label()
            {
                Text = "CursorColor",
                BorderStyle = BorderStyle.FixedSingle,
                TextAlign = ContentAlignment.MiddleCenter,
                Dock = DockStyle.Top,
                BackColor = Color.Transparent,
                ForeColor = Color.FromArgb(24, 65, 150),
                AutoSize = false,
            };
            Titel.MouseDown += new MouseEventHandler(MouseDownEventHandler);
            Titel.MouseUp += new MouseEventHandler(MouseUpEventHandler);
            Titel.MouseMove += new MouseEventHandler(MouseMoveEventHandler);
            Titel.DoubleClick += new EventHandler(ExitHandler);
            this.MainPanel.Controls.Add(Titel);

            // 启动线程
            MainThread = new Thread(MyThreadMethod);
            MainThread.IsBackground = true; // 设置为后台线程，确保随主线程退出
            MainThread.Start();
        }

        private void MouseDownEventHandler(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left) // 只处理左键按下
            {
                MouseDownFlag = true;
                clickLocation = e.Location; // 记录鼠标点击的位置 - (在窗体中的位置)
            }
        }
        private void MouseUpEventHandler(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left) // 只处理左键释放
            {
                MouseDownFlag = false;
            }
        }
        private void MouseMoveEventHandler(object sender, MouseEventArgs e)
        {
            if (MouseDownFlag) // 如果鼠标按下
            {
                Point newLocation = this.PointToScreen(new Point(e.X, e.Y));
                newLocation.Offset(-clickLocation.X, -clickLocation.Y);
                this.Location = newLocation;
            }
        }
        private System.Drawing.Image ResizeImage(System.Drawing.Image originalImage, Size newSize)
        {
            Bitmap resizedImage = new Bitmap(newSize.Width, newSize.Height);
            using (Graphics graphics = Graphics.FromImage(resizedImage))
            {
                graphics.DrawImage(originalImage, new Rectangle(0, 0, newSize.Width, newSize.Height));
            }
            return resizedImage;
        }

        private void ExitHandler(object sender, EventArgs e)
        {
            systemexit = true; // 通知后台线程退出
            MainThread.Join(1000); // 等待线程最多1000毫秒
            Application.Exit();
        }

        private static void MyThreadMethod()
        {
            try
            {
                Point cursorPosition;
                Color cursorColor;
                while (!systemexit)
                {
                    cursorPosition = Cursor.Position;
                    cursorColor = GetColorFromScreen(cursorPosition);

                    // 更新鼠标位置
                    UpdateLabel(CursorPositionLab, $"Cursoe: {cursorPosition.X}, {cursorPosition.Y}");

                    // 更新颜色信息 string.Format("{0:D4}", cursorColor.R)
                    string R = string.Format("{0:D3}", cursorColor.R);
                    string G = string.Format("{0:D3}", cursorColor.G);
                    string B = string.Format("{0:D3}", cursorColor.B);
                    UpdateLabel(CursorColorRGBLab, $"RGB:   {R}, {G}, {B}");

                    string hexColor = $"Hex:   #{cursorColor.R:X2}{cursorColor.G:X2}{cursorColor.B:X2}";
                    UpdateLabel(CursorColorHexLab, hexColor);

                    Thread.Sleep(100); // 每100毫秒更新一次
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"异常类型：{ex.GetType().Name}\n异常消息：{ex.Message}\n堆栈跟踪：{ex.StackTrace}");
            }
        }
        private static void UpdateLabel(Label label, string text)
        {
            if (label != null && !label.IsDisposed && label.InvokeRequired)
            {
                label.Invoke(new Action(() => UpdateLabel(label, text)));
            }
            else if (label != null && !label.IsDisposed)
            {
                label.Text = text;
            }
        }

        private static Color GetColorFromScreen(Point mousePosition)
        {
            using (Bitmap bitmap = new Bitmap(1, 1))
            {
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    g.CopyFromScreen(mousePosition, new Point(0, 0), new Size(1, 1));
                }
                return bitmap.GetPixel(0, 0);
            }
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
